// import axios from '@/api/axios'
import {Toast, MessageBox} from 'mint-ui'
import apiUrl from "@/api/apiUrl"

export default {
    register () {
        this.$set(this, 'logining', true)
        this.validator.validateAll({
            username: this.username,
            password: this.password
        }).then((result) => {
            if (result) {
                this.todo()
            } else {
                this.error()
            }
        });
    },
    todo () {
        if (this.password !== this.password2) {
            this.error('两次密码不一致')
        } else {
            let params = new URLSearchParams()
            params.append('user_name', this.username)
            params.append('password', this.password)
            // params.append('mobile', this.mobile)
            params.append('type', this.userType === '普通用户' ? 1 : 2)
            this.$http.post(apiUrl.api.register, params).then(result => {
                if (result.data.code === 1) {
                    this.error(result.data.msg)
                } else {
                    this.$set(this, 'logining', false)
                    MessageBox('提示', '注册成功, 马上登录');
                    this.$router.replace({name: 'login'})
                }
            }).catch(err => {
                console.log(err)
            })
        }
    },
    error (msg = null) {
        let instance = Toast(msg ? msg : this.errors.all()[0])
        setTimeout(() => {
            instance.close();
            this.$set(this, 'logining', false)
        }, 1000);
    }
}